package explorer;

import java.awt.FlowLayout;
import java.io.File;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import sun.awt.geom.AreaOp.AddOp;

import explorer.abstractFactory.ProxyFactoryLoader;

public class MainPanel extends JPanel implements Observer{

	private ElementsPanel elementsPanel= new ElementsPanel();
	private int n=0;
	private File[] contents;
	private PathLoader files;
	private ProxyFactoryLoader factory;
	
	public MainPanel(PathLoader files, ProxyFactoryLoader factory) {
		
		factory.addObserver(this);
		files.addObserver(this);
		this.files=files;
		this.factory=factory;
		
		contents= files.listContents();
		n=contents.length;
		setLayout(new FlowLayout(/*50*/FlowLayout.LEFT));
		
		for (int i = 0; i < n; i++) {
		
			add(elementsPanel.elementsPanel(i, contents[i], factory));
			
		}
	
	
	}
	
	public void rebuild(){
		
		this.removeAll();
		
		contents= files.listContents();
		n=contents.length;
		setLayout(new FlowLayout(/*50*/FlowLayout.LEFT));
		
		for (int i = 0; i < n; i++) {
		
			add(elementsPanel.elementsPanel(i, contents[i], factory));
		
		}
		
		this.validate();
		
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		rebuild();
	}
	
}
